/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.RemindType;
import com.yami.shop.bean.model.NotifyLog;
import com.yami.shop.bean.model.SmsRechargeLog;
import com.yami.shop.bean.param.OrderParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.NotifyLogService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 *
 *
 * @author lhd
 * @date 2020-07-05 16:19:10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/multishop/notifyLog" )
public class NotifyLogController {

    private final NotifyLogService notifyLogService;
    private final MapperFacade mapperFacade;

    /**
     * 分页查询
     * @param page 分页对象
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<NotifyLog>> getSmsRechargeLogPage(PageParam<NotifyLog> page, OrderParam orderParam) {
        return ResponseEntity.ok(notifyLogService.page(page, new LambdaQueryWrapper<NotifyLog>()
                .eq(NotifyLog::getShopId, SecurityUtils.getShopUser().getShopId())
                .eq(NotifyLog::getRemindType, RemindType.SMS.value())
                .gt(Objects.nonNull(orderParam.getStartTime()),NotifyLog::getCreateTime,orderParam.getStartTime())
                .lt(Objects.nonNull(orderParam.getEndTime()),NotifyLog::getCreateTime,orderParam.getEndTime())
                .orderByDesc(NotifyLog::getCreateTime)));
    }


    /**
     * 查询未读消息数量
     */
    @GetMapping("/unReadCount" )
    public ResponseEntity<Integer> getNotifyCount() {
        return ResponseEntity.ok(notifyLogService.count(new LambdaQueryWrapper<NotifyLog>()
                .eq(NotifyLog::getRemindId, SecurityUtils.getShopUser().getShopId())
                .eq(NotifyLog::getRemindType, 1)
                .ge(NotifyLog::getSendType, 100)
                .eq(NotifyLog::getStatus,0)));
    }


    /**
     * 查询未读消息列表
     */
    @GetMapping("/unReadCountList" )
    public ResponseEntity<IPage<NotifyLog>> getUnReadCountList(PageParam<NotifyLog> page,NotifyLog notifyLog) {
        IPage<NotifyLog> notifyLogs = notifyLogService.page(page,new LambdaQueryWrapper<NotifyLog>()
                .eq(NotifyLog::getRemindId, SecurityUtils.getShopUser().getShopId())
                .eq(NotifyLog::getRemindType, 1)
                .ge(NotifyLog::getSendType, 100)
                .orderByAsc(NotifyLog::getStatus)
                .orderByDesc(NotifyLog::getCreateTime));

        List<NotifyLog> recordsDb = notifyLogs.getRecords();
        List<NotifyLog> records = new ArrayList<>();
        for (NotifyLog record : recordsDb) {
            NotifyLog remind = mapperFacade.map(record,NotifyLog.class);
            record.setStatus(1);
            records.add(remind);
        }
        notifyLogs.setRecords(records);
        if(CollectionUtils.isNotEmpty(recordsDb)) {
            notifyLogService.updateBatchById(recordsDb);
        }
        return ResponseEntity.ok(notifyLogs);
    }
}
